package com.dtp.extension.limiter.redis.ratelimiter;

import com.dtp.common.util.CommonUtil;
import com.dtp.extension.limiter.redis.em.RateLimitEnum;

import java.util.Arrays;
import java.util.List;

/**
 * 滑动窗口限流器
 *
 * @author yanhom
 * @since 1.0.8
 *
 * @remark muse
 **/
public class SlidingWindowRateLimiter extends AbstractRedistRateLimiter {

    public SlidingWindowRateLimiter() {
        super(RateLimitEnum.SLIDING_WINDOW.getScriptName()); // 对应执行脚本：sliding_window_rate_limiter.lua
    }

    @Override
    public List<String> getKeys(final String key) {
        String cacheKey = CommonUtil.getInstance().getServiceName() + ":" + PREFIX + ":" + key; // [服务名]:dtp:[key]
        String memberKey = CommonUtil.getInstance().getIp() + ":" + COUNTER.incrementAndGet();  // [服务IP]:[自增号]
        return Arrays.asList(cacheKey, memberKey); // keys=[cacheKey, memberKey]
    }
}
